const express = require("express");
var router = express.Router();
const db = require("../model/db");

// 作者：雷浩然
// 添加一个电影
router.post("/", async (req, res) => {
    const data = await db.Movie.create(req.body);
    res.send({
        code: 0,
        message: "",
        data
    });
});

// 查询所有电影 不分页
router.get("/www/", async (req, res) => {
    const data = await db.Movie.find();
    const count = parseInt(await db.Movie.count());
    res.send({
        code: 0,
        message: "",
        count,
        data
    });
})

// 作者：雷浩然
// 删除一个电影
router.delete("/:id", async (req, res) => {
    const data = await db.Movie.deleteOne({
        _id: req.params.id
    });
    res.send({
        code: 0,
        message: "",
        data
    });
});

// 作者：雷浩然
// 修改一个电影
router.put("/:id", async (req, res) => {
    const data = await db.Movie.updateOne({
        _id: req.params.id
    }, req.body);
    res.send({
        code: 0,
        message: "",
        data
    });
})
// 作者：雷浩然
router.get("/", async (req, res) => {
    //处理参数
    const query = Object.assign({},
        {
            type: "",
            area: "",
            charactor: "none",
            upDate: "",
            key: "",
            page: 1,
            limit: 12,
            sort: "_id"
        }, req.query);
    //根据参数完成条件对象
    const condition = {};
    //类型的条件
    condition.type = { $regex: new RegExp(query.type) };
    //区域的条件
    condition.area = { $regex: new RegExp(query.area) };
    //上映时间的条件
    condition.upDate = { $regex: new RegExp(query.upDate) };
    //电影上映类型的条件
    if (query.charactor == "isHot") {
        condition.isHot = true;
    } else if (query.charactor == "isComing") {
        condition.isComing = true;
    } else if (query.charactor == "isClassic") {
        condition.isClassic = true;
    }
    //关键字
    const keyReg = new RegExp(query.key);
    condition.$or = [
        { name: { $regex: keyReg } },
        { ename: { $regex: keyReg } }
    ];
    //查询
    const data = await db.Movie.find(condition).sort(query.sort).skip((query.page - 1) * query.limit).limit(query.limit * 1);
    const count = await db.Movie.countDocuments(condition);
    res.send({
        code: 0,
        message: "",
        count,
        data
    })
})

//查询所有电影
router.get("/www", async (req, res) => {

    const data = await db.Movie.find()
    const count = parseInt(await db.Movie.count());
    res.send({
        code: 0,
        message: "",
        count,
        data
    });
})



// 作者：雷浩然
// 通过ID查询单个电影
router.get("/:id", async (req, res) => {
    const data = await db.Movie.findById(req.params.id);
    res.send({
        code: 0,
        message: "",
        data
    });
})

//作者：唐松林
// 查询演员
router.get("/actor/:id", async (req, res) => {
    const data = await db.Movie.find({
        _id: db.ObjectId(req.params.id)
    }, req.body);
    res.send(data[0].actor);
});


//作者：唐松林
// 查询导演
router.get("/director/:id", async (req, res) => {
    const data = await db.Movie.find({
        _id: db.ObjectId(req.params.id)
    }, req.body);
    res.send(data[0].director);
});
//作者：唐松林
//删除导演
router.delete("/director/:id", async (req, res) => {
    const data = await db.Movie.findById(req.params.id);
    data.director.forEach(d => {
        if (d._id == req.body.key) {
            data.director.remove(d);
        }
    });
    await db.Movie.updateOne({ _id: req.params.id }, data);
    res.send(true);
});

//作者：雷浩然
//删除演员
// 修改BUG：post请求路径错误
// 修改BUF：director没有改成actor
router.delete("/actor/:id", async (req, res) => {
    const data = await db.Movie.findById(req.params.id);
    data.actor.forEach(d => {
        if (d._id == req.body.key) {
            data.actor.remove(d);
        }
    });
    const del = await db.Movie.updateOne({ _id: req.params.id }, data);
    res.send(true);
});

//作者：唐松林
// 查询剧照
router.get("/images/:id", async (req, res) => {
    const data = await db.Movie.find({
        _id: db.ObjectId(req.params.id)
    }, req.body);
    res.send(data[0].images);
});
//作者：唐松林
//剧照添加
router.post("/images/:id", async (req, res) => {
    let data = await db.Movie.findById(req.params.id);
    console.log(req.body.images);
    data.images.push(req.body.images);
    await db.Movie.updateOne({ _id: db.ObjectId(req.params.id) }, data);
    res.send(true);
})

//作者：唐松林
//剧照数组删除
router.put("/images/:id", async (req, res) => {
    const data = await db.Movie.findById(req.params.id);
    data.images.pop(req.body.key)
    await db.Movie.updateOne({ _id: req.params.id }, data);
    res.send(true);
})



//作者：雷浩然
//添加导演
router.post("/director/:id", async (req, res) => {
    let data = await db.Movie.findById(req.params.id);
    data.director.push(req.body);
    await db.Movie.updateOne({ _id: db.ObjectId(req.params.id) }, data);
    res.send(true);
})

//作者：雷浩然
//添加演员
router.post("/actor/:id", async (req, res) => {
    let data = await db.Movie.findById(req.params.id);
    data.actor.push(req.body);
    await db.Movie.updateOne({ _id: db.ObjectId(req.params.id) }, data);
    res.send(true);
})

module.exports = router;

